package com.shqcjd.yunweiyanoperator.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.model.pojo.MsgBean;
import com.shqcjd.yunweiyanoperator.ui.adapter.MessageListAdapter;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.Config;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 消息页面
 */
public class MsgActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_include_add)
    TextView tvIncludeAdd;
    @BindView(R.id.rv_msg_list)
    RecyclerView rvMsgList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private View notDataView;
    private MessageListAdapter messageListAdapter;

    private static final int PAGE_SIZE = Config.PAGE_TOTAL;
    private int mNextRequestPage = 1;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_msg;
    }

    @Override
    public void initView() {
        tvTitle.setText("消息");
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvMsgList.getParent(), false);
        initAdapter();
    }

    private void initAdapter() {


        messageListAdapter = new MessageListAdapter();
        rvMsgList.setLayoutManager(new LinearLayoutManager(this));
        rvMsgList.setAdapter(messageListAdapter);

        swipeLayout.setOnRefreshListener(this);
        messageListAdapter.setOnLoadMoreListener(this,rvMsgList);
    }

    @OnClick(R.id.btn_finish)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void initData() {
        refresh();
    }



    @Override
    public void onRefresh() {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    private void loadMore() {
    }

    private void refresh() {
        mNextRequestPage = 1;

    }

    private void setData(boolean isRefresh, List data) {

        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            if (size > 0) {
                messageListAdapter.setNewData(data);
            } else {
                messageListAdapter.setNewData(null);
                messageListAdapter.setEmptyView(notDataView);
            }
        } else {
            if (size > 0) {
                messageListAdapter.addData(data);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            messageListAdapter.loadMoreEnd(isRefresh);
        } else {
            messageListAdapter.loadMoreComplete();
        }
    }

    public void showRefreshView(Boolean refresh) {
        swipeLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(refresh);
            }
        });
    }
}
